import java.awt.Dimension;
import java.util.Iterator;

import com.terrapages.wmsc4j.bean.BBox;
import com.terrapages.wmsc4j.bean.Pos;
import com.terrapages.wmsc4j.layer.StaticLayer;
import com.terrapages.wmsc4j.layer.resolution.Resolution;
import com.terrapages.wmsc4j.layer.resolution.ResolutionSet;
import com.terrapages.wmsc4j.layer.tile.ImageMimeType;
import com.terrapages.wmsc4j.layer.tile.Tile;
import com.terrapages.wmsc4j.layer.tile.TileFormat;

import junit.framework.TestCase;


public class LayerTests extends TestCase {
	public void testIterator() throws Exception {
		
		ResolutionSet resolutions = new ResolutionSet(new Resolution(0.703125),20);
		BBox coverage = new BBox(-180,-90,180,90);
		StaticLayer layer = new StaticLayer(
				null,resolutions,
				new TileFormat(new Dimension(256,256), ImageMimeType.PNG),
				coverage,
				new Pos(-180,-90),
				"UnprojectedGeoserver"
				);
		
		Iterator<Tile> iterator = layer.iterator();
		assertTrue(iterator.hasNext());
		Tile tile = iterator.next();
		assertEquals(0,tile.getZoom());
		assertEquals(0,tile.getYIndex());
		assertEquals(0,tile.getXIndex());
	}
	
	public void testGetMaximumTile0() throws Exception {
		ResolutionSet resolutions = new ResolutionSet(new Resolution(0.703125),20);
		BBox coverage = new BBox(-180,-90,180,90);
		StaticLayer layer = new StaticLayer(
				null,resolutions,
				new TileFormat(new Dimension(256,256), ImageMimeType.PNG),
				coverage,
				new Pos(-180,-90),
				"UnprojectedGeoserver"
				);
		
		Tile tile = layer.getMaximumTile(layer.getResolutions().getHighestResolution());
		assertEquals(0,tile.getZoom());
		assertEquals(0,tile.getYIndex());
		assertEquals(1,tile.getXIndex());
	}
	
	public void testGetMaximumTile1() throws Exception {
		ResolutionSet resolutions = new ResolutionSet(new Resolution(0.703125),20);
		BBox coverage = new BBox(-180,-90,180,90);
		StaticLayer layer = new StaticLayer(
				null,resolutions,
				new TileFormat(new Dimension(256,256), ImageMimeType.PNG),
				coverage,
				new Pos(-180,-90),
				"UnprojectedGeoserver"
				);
		Tile tile = layer.getMaximumTile(layer.getResolution(1));
		assertEquals(1,tile.getZoom());
		assertEquals(1,tile.getYIndex());
		assertEquals(3,tile.getXIndex());
	}
	
	public void testGetMaximumTile2() throws Exception {
		ResolutionSet resolutions = new ResolutionSet(new Resolution(0.703125),20);
		BBox coverage = new BBox(-180,-90,180,90);
		StaticLayer layer = new StaticLayer(
				null,resolutions,
				new TileFormat(new Dimension(256,256), ImageMimeType.PNG),
				coverage,
				new Pos(-180,-90),
				"UnprojectedGeoserver"
				);
		
		Tile tile = layer.getMaximumTile(layer.getResolution(2));
		assertEquals(2,tile.getZoom());
		assertEquals(3,tile.getYIndex());
		assertEquals(7,tile.getXIndex());
	}
	
	public void testGetMaximumTile3() throws Exception {
		ResolutionSet resolutions = new ResolutionSet(new Resolution(0.703125),20);
		BBox coverage = new BBox(-180,-90,180,90);
		StaticLayer layer = new StaticLayer(
				null,resolutions,
				new TileFormat(new Dimension(256,256), ImageMimeType.PNG),
				coverage,
				new Pos(-180,-90),
				"UnprojectedGeoserver"
				);
		
		Tile tile = layer.getMaximumTile(layer.getResolution(3));
		assertEquals(3,tile.getZoom());
		assertEquals(7,tile.getYIndex());
		assertEquals(15,tile.getXIndex());
	}
	
	public void testGetMaximumTile19() throws Exception {
		ResolutionSet resolutions = new ResolutionSet(new Resolution(0.703125),20);
		BBox coverage = new BBox(-180,-90,180,90);
		StaticLayer layer = new StaticLayer(
				null,resolutions,
				new TileFormat(new Dimension(256,256), ImageMimeType.PNG),
				coverage,
				new Pos(-180,-90),
				"UnprojectedGeoserver"
				);
		
		Tile tile = layer.getMaximumTile(layer.getResolution(19));
		assertEquals(19,tile.getZoom());
		assertEquals(524287,tile.getYIndex());
		assertEquals(1048575,tile.getXIndex());
	}
	
	public void testGetMinimumTile19() throws Exception {
		ResolutionSet resolutions = new ResolutionSet(new Resolution(0.703125),20);
		BBox coverage = new BBox(-180,-90,180,90);
		StaticLayer layer = new StaticLayer(
				null,resolutions,
				new TileFormat(new Dimension(256,256), ImageMimeType.PNG),
				coverage,
				new Pos(-180,-90),
				"UnprojectedGeoserver"
				);
		
		Tile tile = layer.getMinimumTile(layer.getResolution(19));
		assertEquals(19,tile.getZoom());
		assertEquals(0,tile.getYIndex());
		assertEquals(0,tile.getXIndex());
	}
}
